﻿using Inaction.Mapping;
using Inaction.Security;
using Inaction.Service.Client.WcfService;
using Inaction.ServiceProxy;
using Inaction.Util;
using System;
using System.Security.Principal;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Inaction.Service.Client.Proxy
{
    public class WcfServiceProxy : IProxy
    {
        #region 属性
        private WcfService.HostClient host = null;
        #endregion

        public WcfServiceProxy()
        {
            Binding binding = null;
            if (AppConfig.Instance.ServerUrl.StartsWith("net.tcp:", StringComparison.OrdinalIgnoreCase))
            {
                binding = new NetTcpBinding(SecurityMode.None) { MaxReceivedMessageSize = int.MaxValue };
            }
            else
            {
                binding = new WSHttpBinding(SecurityMode.None) { MaxReceivedMessageSize = int.MaxValue };
            }
            string url = AppConfig.Instance.ServerUrl;
            binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
            host = new WcfService.HostClient(binding, new EndpointAddress(url));
        }

        public void Login()
        {
            var response = host.Login(new Request(new RequestData()));
            var responseData = response.GetObject<Inaction.ServiceProxy.ResponseData>();
            if (responseData.Exception != null)
            {
                throw responseData.Exception;
            }
            Inaction.Security.Principal.Instance = responseData.Data as Security.Principal;
        }
        public object Query(Type entityType, SqlStatement sql)
        {
            var response = host.Query(new Request(
               new RequestData(sql)
               {
                   Assembly = entityType.Assembly.GetName().Name,
                   FullName = entityType.FullName
               }));
            var data = response.GetObject<ResponseData>();
            if (data.Exception != null)
                throw data.Exception;
            return data.Data;
        }
        public object Save(Inaction.Core.ISavable obj)
        {
            var response = host.Save(new Request(new RequestData(obj)));
            var data = response.GetObject<ResponseData>();
            if (data.Exception != null)
                throw data.Exception;
            return data.Data; ;
        }
        public object Excute(Inaction.SqlCommand command)
        {
            var response = host.Excute(new Request(new RequestData(command)));
            var data = response.GetObject<ResponseData>();
            if (data.Exception != null)
                throw data.Exception;
            return data.Data;
        }
        public object Invoke(Inaction.RemotingMethod method)
        {
            var response = host.Invoke(new Request(new RequestData(method)));
            var data = response.GetObject<ResponseData>();
            if (data.Exception != null)
                throw data.Exception;
            return data.Data;
        }

        #region 方法
       
        #endregion
    }
}